package org.spring.boot.fusion.fallback;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class FallbackServiceImpl implements ClientHttpResponse{

	@Autowired
	private ObjectMapper objectMapper;
	HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
	
	public InputStream getBody() throws IOException {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("state", httpStatus.value());
		hashMap.put("message", "请求异常");
		String writeValueAsString = objectMapper.writeValueAsString(hashMap);
		return new ByteArrayInputStream(writeValueAsString.getBytes("UTF-8"));
	}

	public HttpHeaders getHeaders() {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		return headers;
	}

	public int getRawStatusCode() throws IOException {
		return httpStatus.value();
	}

	public HttpStatus getStatusCode() throws IOException {
		return httpStatus;
	}

	public String getStatusText() throws IOException {
		return httpStatus.name();
	}

	public void close() {}

}